import { Pages, LoadFrom, DisposeDeliver, CommunicateResume, CommunicateList, Export, CollectResume } from '@/api/resume/resume'
import store from '@/store'

const state = {
  controllerName: 'resume',
  dataList: {
    listLoading: false,
    list: [],
    replayList: [],
    deliverStateList: ['待处理', '已沟通', '已邀面试', '已放弃'],
    defaultProps: {
      children: 'departmentList',
      key: 'id',
      label: 'name'
    },
    searchForm: {
      // JobId: 'A150B6E1-9AF4-4BBC-AE2F-11385C2CA9CF',
      pageSize: 10,
      pageIndex: 1,
      dataCount: 0,
      pageCount: 0
    }
  },
  form: {
    formState: false,
    formDepartmentState: false,
    model: {},
    replayState: false,
    replaymodel: {},
    inviteState: false,
    invitemodel: { Reason: '' },
    deliverState: false
  }
}
const mutations = {
  pageSizeChange(state, par) {
    state.dataList.searchForm.pageSize = par
    this.dispatch(`resume/${state.controllerName}/findList`)
  },
  currentPageChange(state, par) {
    state.dataList.searchForm.pageIndex = par
    this.dispatch(`resume/${state.controllerName}/findList`)
  }
}

const actions = {
  loadNewForm(context) {
    var _state = context.state
    _state.form.model = {}
    _state.form.formState = true
  },
  // 加载表单
  loadForm(context, par) {
    var _state = context.state
    console.log('加载表单参数', par)
    var companyId = store.getters.companyId
    console.log(companyId, '公司id')

    LoadFrom({ ID: par.Id, type: 1, CompanyId: companyId })
      .then(response => {
        const { Data } = response
        console.log(Data)
        _state.form.model = Data
        _state.form.model.deliverId = par.DeliverId
        _state.form.model.deliverState = par.DeliverState
        _state.form.formState = true
      })
      .catch(error => {
        console.log(error)
      })
  },
  findList(context, par) {
    var _state = context.state
    _state.dataList.listLoading = true
    Pages(_state.dataList.searchForm)
      .then(res => {
        const { Data } = res
        console.log(Data)
        _state.dataList.list = Data.List
        _state.dataList.searchForm.pageIndex = Data.PageIndex
        _state.dataList.searchForm.pageSize = Data.PageSize
        _state.dataList.searchForm.dataCount = Data.Count
        _state.dataList.searchForm.pageCount = Data.Pages
        _state.dataList.listLoading = false
      })
      .catch(error => {
        console.log(error)
      })
  },
  loadInvite(context, par) {
    var _state = context.state
    _state.form.invitemodel.deliverId = par.DeliverId
    _state.form.invitemodel.deliverState = par.DeliverState
    _state.form.inviteState = true
  },
  // 处理简历-邀约放弃已沟通
  disposeDeliver(context, par) {
    var _state = context.state
    if (par.Reason === '' || par.Reason === undefined) {
      global.tools.msg('请输入原因!', '错误')
      return
    }
    console.log('处理简历', par)
    DisposeDeliver(par)
      .then(response => {
        const { Data } = response
        console.log(Data)
        _state.form.resState = Data
        if (_state.form.resState) {
          global.tools.msg('操作成功!', '成功')
          _state.form.inviteState = false
          context.dispatch('findList')
        } else {
          global.tools.msg('操作失败!', '错误')
        }
      })
      .catch(error => {
        console.log(error)
      })
  },
  loadReplay(context, par) {
    var _state = context.state
    _state.form.replaymodel.JobId = par.JobId
    _state.form.replaymodel.BeUserId = par.Id
    _state.form.replaymodel.Type = 1
    _state.form.replayState = true
    if (par.DeliverState === 0) {
      _state.form.deliverState = true
      // 已沟通
      _state.form.invitemodel.ID = par.DeliverId
      _state.form.invitemodel.Reason = '已沟通'
      _state.form.invitemodel.Type = 1
    }
  },
  // 简历留言
  replay(context, par) {
    var _state = context.state
    console.log('简历留言', par)
    CommunicateResume(par)
      .then(response => {
        const { Data } = response
        console.log(Data)
        _state.form.resState = Data
        if (_state.form.resState) {
          // global.tools.msg('操作成功!', '成功')
          _state.form.replayState = false
          if (_state.form.deliverState) {
            context.dispatch('disposeDeliver', _state.form.invitemodel)
          } else {
            context.dispatch('findList')
          }
        } else {
          global.tools.msg('操作失败!', '错误')
        }
      })
      .catch(error => {
        console.log(error)
      })
  },
  resumeCollect(context, par) {
    var _state = context.state
    console.log('收藏', par)
    CollectResume(par)
      .then(response => {
        const { Data } = response
        console.log(Data)
        _state.form.resState = Data
        if (_state.form.resState) {
          global.tools.msg('操作成功!', '成功')
          _state.form.formState = false
          // ontext.dispatch('findList')
        } else {
          global.tools.msg('操作失败!', '错误')
        }
      })
      .catch(error => {
        console.log(error)
      })
  },
  communicateList(context, par) {
    var _state = context.state
    _state.dataList.listLoading = true
    var _partemp = _state.dataList.searchForm
    _partemp.JobId = par.JobId
    _partemp.BeUserId = par.Id
    _partemp.Type = 1

    CommunicateList(_partemp)
      .then(res => {
        const { Data } = res
        console.log(Data)
        // _state.dataList.replayList = Data.List
        // _state.dataList.searchForm.pageIndex = Data.PageIndex
        // _state.dataList.searchForm.pageSize = Data.PageSize
        // _state.dataList.searchForm.dataCount = Data.Count
        // _state.dataList.searchForm.pageCount = Data.Pages
        _state.dataList.listLoading = false
      })
      .catch(error => {
        console.log(error)
      })
  },
  exportExcel(context, par) {
    var _state = context.state
    Export(par)
      .then(response => {
        // const { Data } = res
        console.log(response.data, 'ExportDoc')
        // var blob = response.body
        if (response.status === 200) {
          const type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
          // 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'// xhr.getResponseHeader('Content-Type')
          var fileName = _state.form.model.UserName + '的简历.doc'
          const blob = new Blob([response.data], { type: type })
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
          /*
          * IE workaround for "HTML7007: One or more blob URLs were revoked by closing
          * the blob for which they were created. These URLs will no longer resolve as
          * the data backing the URL has been freed."
          */
            window.navigator.msSaveBlob(blob, fileName)
          } else {
            const URL = window.URL || window.webkitURL
            const objectUrl = URL.createObjectURL(blob)
            if (fileName) {
              var a = document.createElement('a')
              // safari doesn't support this yet
              if (typeof a.download === 'undefined') {
                window.location = objectUrl
              } else {
                a.href = objectUrl
                a.download = fileName
                document.body.appendChild(a)
                a.click()
                a.remove()
              }
            } else {
              window.location = objectUrl
            }
          }
        }
      })
      .catch(error => {
        console.log(error)
      })
  },
  // exportExcel(context, par) {
  //   var _state = context.state
  //   // this.$nextTick(()=>{
  //   // var template = this.$refs.resume.innerHTML
  //   console.log(par, 'getHtmlContent')
  //   console.log(par.$el.innerHTML, 'getHtmlContent===')
  //   // })
  //   // var html = getHtmlContent();
  //   // 构造blob文件流
  //   var html_ = new Blob([par.$el.innerHTML],{ "type" : "text/html;charset=utf-8" })
  //   var formdata = new FormData();
  //   formdata.append('file', html_, `sdf.html`);//sdf.html是设置文件名


  //   ExportDoc(formdata)
  //     .then(response => {
  //       // const { Data } = res
  //       console.log(response.data, 'ExportDoc')
  //       // var blob = response.body
  //       if (response.status === 200) {
  //         // application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'}); //application/vnd.openxmlformats-officedocument.wordprocessingml.document这里表示doc类型
  //         const type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'//'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'// xhr.getResponseHeader('Content-Type')
  //         var fileName = _state.form.model.UserName + '的简历.doc'
  //         const blob = new Blob([response.data], { type: type })
  //         if (typeof window.navigator.msSaveBlob !== 'undefined') {
  //         /*
  //         * IE workaround for "HTML7007: One or more blob URLs were revoked by closing
  //         * the blob for which they were created. These URLs will no longer resolve as
  //         * the data backing the URL has been freed."
  //         */
  //           window.navigator.msSaveBlob(blob, fileName)
  //         } else {
  //           const URL = window.URL || window.webkitURL
  //           const objectUrl = URL.createObjectURL(blob)
  //           if (fileName) {
  //             var a = document.createElement('a')
  //             // safari doesn't support this yet
  //             if (typeof a.download === 'undefined') {
  //               window.location = objectUrl
  //             } else {
  //               a.href = objectUrl
  //               a.download = fileName
  //               document.body.appendChild(a)
  //               a.click()
  //               a.remove()
  //             }
  //           } else {
  //             window.location = objectUrl
  //           }
  //         }
  //       }
  //     })
  //     .catch(error => {
  //       console.log(error)
  //     })
  // },
  handleNodeClick(context, node) {
    var _state = context.state
    _state.form.model.departmentId = node.id
    _state.form.model.departmentName = node.name
    _state.form.formDepartmentState = false
  },
  handleSuccess(context, res) {
    console.log(res)
    if (res.Status === 1) {
      var _state = context.state
      _state.form.model.HeaderImage = res.Data.Url
      console.log('_state.form.model.HeaderImage', _state.form.model.HeaderImage)
    } else {
      global.tools.msg('上传失败，请重新上传！', '提示')
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
